package com.giscloud.generator.controller;

import com.google.common.base.Stopwatch;
import com.giscloud.generator.entity.DatabaseEntity;
import com.giscloud.generator.service.DatabaseService;
import com.giscloud.generator.utils.PageUtils;
import com.giscloud.generator.utils.Query;
import com.giscloud.generator.utils.R;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author:giscloud
 * @Description:
 * @Date: 15:02 2019/11/21
 * @Modified By:
 */
@RestController
@RequestMapping("/sys/database")
public class DatabaseController {
    private final DatabaseService databaseService;

    @Autowired
    public DatabaseController(DatabaseService databaseService){
        this.databaseService = databaseService;
    }

    @RequestMapping("/select1")
    public R select(){
        List<Map<String, Object>> maps = databaseService.selectDatabases1();
        return R.ok().put("databases", maps);
    }

    @RequestMapping("/select")
    public R select1(){
        List<Map<String, Object>> maps = databaseService.selectDatabases();
        return R.ok().put("data", maps);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils pageUtil = databaseService.list(new Query(params));
        return R.ok().put("page", pageUtil);
    }

    /**
     * 更新
     * @param databaseEntity
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody DatabaseEntity databaseEntity){
        if(databaseService.update(databaseEntity) > 0){
            return R.ok("修改成功");
        }
        return R.error("修改失败");
    }

    @PostMapping("/insert")
    public R insert(@RequestBody DatabaseEntity databaseEntity){
        databaseEntity.setCreateDt(new Date());
        databaseEntity.setModifiedDt(new Date());
        if(databaseService.insert(databaseEntity) > 0){
            return R.ok("提交成功");
        }
        return R.error("提交失败");
    }

    @RequestMapping("/delete/{id}")
    public R delete(@PathVariable(name = "id") String id ){
        String[] ids = id.split(",");
        long[] longs = Arrays.stream(ids).mapToLong(Long::parseLong).toArray();
        if(databaseService.delete(longs) > 0){
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }

    public static void main(String[] args) {
        Stopwatch stopwatch = Stopwatch.createStarted();
        Triple<Integer,Integer,String> triple = new ImmutableTriple<>(1,2,"3");

        System.out.println(triple);
        long second = stopwatch.elapsed(TimeUnit.NANOSECONDS);

        System.out.println(second);
    }
}
